package com.kgc.controller;

import com.kgc.pojo.*;
import com.kgc.service.UserService;
import com.kgc.util.FtpUtil;
import com.kgc.util.RedisUtil;
import com.kgc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;


/**
 * 用户表的控制器
 * by-南宫问天
 */

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    RedisUtil redisUtil;

    public void test(){
        System.out.println("测试");
    }

    @RequestMapping("getUserDetailByToken_TL")
    public Object getUserDetailByToken_TL(HttpServletRequest request) {
        String token = request.getHeader("token");
        User user = (User) redisUtil.get(token);
        if (user != null) {
            UserDetail userDetail = userService.getUserDetail(user.getUserdetailid());
            return new Result(userDetail, "获取成功");
        }
        return new Result(false, "获取失败");
    }

    @GetMapping("getUserByToken_TL")
    public Object getUserByToken_TL(HttpServletRequest request){
        String token = request.getHeader("token");
        User user = (User) redisUtil.get(token);
        if(user == null){
            return new Result(false,"获取用户失败");
        }
        return new Result(user,"获取用户成功");
    }

    @RequestMapping("getUserByLogin_CY")
    public Object getUserByLogin(HttpServletRequest request) {
        String token = request.getHeader("token");
        User user = (User) redisUtil.get(token);
        if (user != null) {

            UserDetail userDetail = userService.getUserDetail(user.getUserdetailid());
            return new Result(userDetail, "获取用户状态成功");
        }
        return new Result(false, "获取用户状态成功");
    }

    @PostMapping("uploadUserPic")
    public Object uploadUserPic(@RequestParam("multipartFile") MultipartFile multipartFile) throws IOException {
        //获取文件上传的输入流
        InputStream inputStream = multipartFile.getInputStream();
        String filename = multipartFile.getOriginalFilename();//获取原文件名
        String suffix = filename.substring(filename.lastIndexOf("."));//获取文件后缀
        String newName = UUID.randomUUID() + suffix;

        boolean flag = FtpUtil.uploadFile(newName, inputStream);
        return new Result(newName, "上传文件");
    }

    //保存用户信息
    @PostMapping("updUser_TL")
    public Object updUser_TL(UserDetail userDetail, HttpServletRequest request, @RequestParam(value = "picPath", required = false) MultipartFile picPath) throws IOException {
        String year = request.getParameter("birthY");
        String month = request.getParameter("birthM");
        String day = request.getParameter("birthD");
        userDetail.setBirthdate(year + "-" + month + "-" + day);
        if (userDetail.getSex().equals("1")) {
            userDetail.setSex("男");
        } else {
            userDetail.setSex("女");
        }
        /*上传图片待定 ftp服务器无法搭建*/
       /* if (picPath != null){
            //获取文件上传的输入流
            InputStream inputStream = picPath.getInputStream();
            String filename = picPath.getOriginalFilename();//获取原文件名
            assert filename != null;
            String suffix = filename.substring(filename.lastIndexOf("."));//获取文件后缀
            String newName = UUID.randomUUID() + suffix;
            FtpUtil.uploadFile(newName,inputStream);
        }*/
        int count = userService.updUser_TL(userDetail);
        return new Result(true, "修改" + count + "行");
    }

    //获取省份
    @GetMapping("getProvinces_TL")
    public Object getProvinces_TL(){
        List<Provinces> provinces =  userService.getProvinces_TL();
        return new Result(provinces,"获取省份成功");
    }

    //获取城市
    @GetMapping("getCity_TL")
    public Object getCity_TL(int province) {
        List<City> cities = userService.getCity_TL(province);
        return new Result(cities, "获取城市成功");
    }

    //获取区县
    @GetMapping("getAreas_TL")
    public  Object getAreas_TL(int city){
        List<Areas> areas = userService.getAreas_TL(city);
        return new Result(areas,"获取区县成功");
    }

    //获取街道
    @GetMapping("getStreet_TL")
    public Object getStreet_TL(int area){
        List<Street> streets = userService.getStreet_TL(area);
        return new Result(streets,"获取街道成功");
    }

    //根据用户详情id查询用户收货地址
    @GetMapping("getAddressByUserDetailId_TL")
    public Object getAddressByUserDetailId(int userDetailId) {
        List<UserAddressEx> userAddressEx = userService.getAddressByUserDetailId(userDetailId);
        return new Result(userAddressEx,"查询成功");
    }

    //保存用户地址
    @PostMapping("addUserAddress_TL")
    public Object addUserAddress_TL(UserAddress userAddress){
        int count = userService.addUserAddress_TL(userAddress);
        return new Result(count,"添加成功");
    }

    //用户实名认证
    @PostMapping("updUserDetail_TL")
    public Object updUserDetail_TL(UserDetail userDetail){
        int count = userService.updUserDetail_TL(userDetail);
        return new Result(count,"添加成功");
    }

    //删除用户地址
    @GetMapping("delUserAddress_TL")
    public  Object delUserAddress_TL(int userAddressId){
        int count =  userService.delUserAddress_TL(userAddressId);
        return new Result(count,"删除成功");
    }

    //修改用户登录密码
    @PostMapping("updUserPwd_TL")
    public Object updUserPwd_TL(Integer userid,String newpwd){
        int count = userService.updUserPwd_TL(userid,newpwd);
        return new Result(count,"修改登录密码成功");
    }
}
